package leetcode.editor.cn;

import utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的所有路径
 * @author qulq
 * @date 2024-07-06 15:23:33
 */
public class T257_BinaryTreePaths{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T257_BinaryTreePaths().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

	List<String> ans = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
		ans.clear();
		backtracking(root, "");
		return ans;
    }

	private void backtracking(TreeNode root, String path) {
		if (root.left == null && root.right == null) {
			ans.add(path + root.val + "");
			return;
		}
		if (root.left != null) {
			backtracking(root.left, path + root.val + "->");
		}
		if (root.right != null) {
			backtracking(root.right, path + root.val + "->");
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}